﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using H = functionService.shorts;
using da = functionService.DATECOD;
using fr = HK.FiscalRegistrar.FiscalRegistrar;
using vv = functionService.serfun_city7;
using rz = functionService.serfun_city7.rz;
using nameCod = functionService.nameCod;

namespace city7
{
    namespace rzpt
    {
        enum modeWAED
        {
            work, add, edit, delete, non   
        }
        
    }
    enum colorr { white, blue }
    public partial class FormRzpt : Form
    {
        functionService.prm_city q;
        rzpt.modeWAED currentMode;
        DataTable dts;

        bool pasteTrue;

        int linesPrint, currentPage;

        public FormRzpt(DataTable dt)
        {
            InitializeComponent(); InitializeComponentMy(dt);
           // dataGridView1.CurrentCell = dataGridView1[0, 7];

        }

        private void InitializeComponentMy(DataTable dt)
        {
            q = new functionService.prm_city();

            string s = q.tk((int)ETALON.param.server_1);
            q.con.co(s);
            
            dataGridView1.DataSource = dt;
            set();
            setMode(city7.rzpt.modeWAED.work);

            vv.rz.add_prod_default = "- (888)";
            vv.rz.add_sirT_default = "- (888)";
            vv.rz.currentColumn = 0;
            vv.rz.currentRow = 0;

            dts = dt; pasteTrue = false;
        }
        
        void setMode(rzpt.modeWAED mo)
        {
            currentMode = mo;
            if (mo == city7.rzpt.modeWAED.work)
            { setColor(Color.LightGray, Color.White);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                refresh();
            }
            else if (mo == city7.rzpt.modeWAED.add )
            { setColor(Color.SteelBlue , Color.LightSkyBlue);
            dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

            cellclick_add();
            }
            else if (mo == city7.rzpt.modeWAED.edit )
            { setColor(Color.Green , Color.LightGreen);
            dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            }
            else if (mo == city7.rzpt.modeWAED.delete )
            {
                setColor(Color.Coral, Color.Coral);
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            else if (mo == city7.rzpt.modeWAED.non)
            { setColor(Color.LightGray, Color.White);
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }

        }
        void setColor(Color c1, Color c2)
        {
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
            dataGridView1.Columns[1].DefaultCellStyle.BackColor = c2;
            dataGridView1.Columns[2].DefaultCellStyle.BackColor = c1;
            dataGridView1.Columns[3].DefaultCellStyle.BackColor = c2;
            dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
        }
        void set()
        {
            

            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.LightGray ;
            dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.LightGray;
            dataGridView1.Columns[4].DefaultCellStyle.BackColor = Color.LightGray;

            dataGridView1.Columns[0].Width = 270;
            dataGridView1.Columns[1].Width = 80;
            dataGridView1.Columns[2].Width = 270;
            dataGridView1.Columns[3].Width = 80;
            dataGridView1.Columns[4].Width = 1;

            dataGridView1.Columns[0].HeaderText = "Наименование Продукции";
            dataGridView1.Columns[1].HeaderText = "Кол Выход";
            dataGridView1.Columns[2].HeaderText = "Наименование Сырья";
            dataGridView1.Columns[3].HeaderText = "Кол";
            dataGridView1.Columns[4].HeaderText = "";
        }
        void cellclick()
        {
            if (currentMode == city7.rzpt.modeWAED.add)
            {
                cellclick_add();
            }
            else if (currentMode == city7.rzpt.modeWAED.edit)
            {
                cellclick_edit();
            }
            else if (currentMode == city7.rzpt.modeWAED.delete )
            {
                cellclick_del();
            }
            else if (currentMode == city7.rzpt.modeWAED.work )
            {
                cellclick_work();
            }
            else if (currentMode == city7.rzpt.modeWAED.non )
            {
                cellclick_non();
            }
        }
        private void cellclick_add()
        {
            int i =nameCod.readCod_int(SELECTprod);
            q.UpdateData(rz.sql.add(i, SELECTkolV)); refresh(i);
        }
        
        
        
        
        private void addPF_TehSir()
        {
            DataTable dt = q.selectAny("call `city`.`FOR_add_to__list_tehsir_z__from__list_sc` ()");
            int ic = dt.Rows.Count;
            int iomax = H.i(q.selectScalar("select max(cod)+1 from list_tehsir_z where cod<888;"));
            int i = 0;
            foreach (DataRow r in dt.Rows)
            {
                q.UpdateData
                   (
                   string.Concat
                       (
                         "insert into list_tehsir_z(cod, name, izm) values (", iomax + i, ",'", r[1], "', 'гр');"
                       )
                   );
                q.UpdateData
                   (
                   string.Concat
                       (
                         "call `city`.`rz_update_list_sc_tehsir_z`(",
                        r[0], " , ",
                        iomax + i, ");"
                       )
                   );
                i++;
            }
        }




        void f_add_Poluf(object codo, object nameo, object whereCondition0)
        {
            string name = nameo.ToString().Replace("(", "/").Replace(")", "/");
            int ic = Convert.ToInt32(q.selectScalar(string.Concat("select count(*) as cc from  city.list_tehsir_z WHERE ", whereCondition0, ";")));
            int imm = 1;
            if (ic > 0)
            {
                imm = Convert.ToInt32
                    (q.selectScalar
                    (
                     string.Concat("select max(cod)+1 as codm from city.list_tehsir_z WHERE ", whereCondition0, ";" )
                     )
                    );
            }
            //list_sc_tehsir_z
            string sql = string.Concat("INSERT  city.list_tehsir_z (cod, name, izm) VALUES (",
                imm, ", '", name, "', 'гр');");
            q.UpdateData(sql);

            string sql2 = string.Concat("insert city.list_sc_tehsir_z(codsc, codts) VALUES (", codo, ",", imm, ");");
            q.UpdateData(sql2); 
        }
        void f_del_Poluf(object codo)
        {
            int codts = Convert.ToInt32(q.selectScalar
                (string.Concat("select codts from  city.list_sc_tehsir_z WHERE codsc =", codo, ";")));
            string sql =
                string.Concat("delete from list_tehsir_z WHERE cod =", codts, ";");
            q.UpdateData(sql);
            string sql2 =
                string.Concat("delete from list_sc_tehsir_z WHERE codsc =", codo, ";");
            q.UpdateData(sql);
        }
        void f_edit_Poluf(object codo, object nameo)
        {
            string name = nameo.ToString().Replace("(", "/").Replace(")", "/");
            int codts = Convert.ToInt32(q.selectScalar
               (string.Concat("select codts from  city.list_sc_tehsir_z WHERE codsc =", codo, ";")));
            string sql = string.Concat("update list_tehsir_z  set name = '", name, "' WHERE cod =", codts, ";");
            q.UpdateData(sql);
        }
        
        private string SELECTprod
        {
            get
            {
                string s = "- (888)";
               
                    if (vv.rz.add_prod_default == "- (888)")
                    {
                        DataTable dt = q.selectAny(
                           string.Concat("call `city`.`select_listSc_to_mainListR` ();"));
                        
                        //ETALON.listAnyEdit f =
                        //    new ETALON.listAnyEdit("city", "list_sc", "cod<888",
                        //        f_add_Poluf,
                        //        f_del_Poluf,
                        //        f_edit_Poluf);
                        ETALON.listAnyEdit f =
                           new ETALON.listAnyEdit("city", "list_sc", true );
                        f.ShowDialog();
                        if (f.IsOK)
                        {
                            s = f.sText; vv.rz.add_prod_default = s; toolStripStatusLabel1.Text = s;
                        }
                    }
                    else
                    {
                        s = vv.rz.add_prod_default;
                    }
                return s;
            }
        }

        private double SELECTkolV
        {
            get
            {
                double dd = 0;

                if (vv.rz.add_prod_default == "- (888)")
                {
                    ETALON.FormKolZakaz fkk = 
                        new ETALON.FormKolZakaz("Введите количество");
                    fkk.ShowDialog();
                    if (fkk.IsSelect)
                    {
                        dd = Convert.ToDouble(fkk.valueEnter.Replace(",", ".")); vv.rz.add_kolV_default = dd;
                    }
                }
                else 
                {
                    dd = vv.rz.add_kolV_default;
                }
                
                return dd;
            }
        }
        //DataTable dt = q.selectAny("call `city`.`select_rez`();");
        void  refresh()
        {
            dts = q.selectAny("call `city`.`select_rez`();");
            dataGridView1.DataSource = dts;
        }
       
        void refresh(int codSc0)
        {
            dts = q.selectAny(string.Concat("call `city`.`select_rez_where`(", codSc0 ,");"));
            dataGridView1.DataSource = dts;
        }
        void refresh(string strRLike)
        {
            dts = q.selectAny(string.Concat("call `city`.`select_rez_rlike`( '", strRLike, "');"));
            dataGridView1.DataSource = dts;
        }
        void refreshCopy()
        {
            DataView dv = new DataView(dts);
            dv.RowFilter = string.Concat("name like '%", vv.rz.currentNameCodSc, "%'");
            dataGridView1.DataSource = dv;
        }

        void refresh2(string strRLike)
        {
            /*select
name_cod(n.name, r.codSc) as name, c.kolV,
name_cod(t.name, r.codSirTeh) as nameT, r.kol,
r.cod123
from mainlistr r
left join list_sc n on r.codSc= n.cod
left join list_tehsir_z t on r.codSirTeh= t.cod
left join mainlistr_sc c on r.codSc = c.codSc
where name_cod(n.name, r.codSc) rlike(stext_rlike);*/
           // dts = q.selectAny(string.Concat("call `city`.`select_rez_rlike`( '", strRLike, "');"));
           // dataGridView1.DataSource = dts;

            DataView dv = new DataView(dts);
            //object os = dts.Compute("sum(k)", string.Concat("ct like '%", toolStripTextBox1.Text, "%'"));
            //lable_ss.Text = os.ToString();
            dv.RowFilter = string.Concat("nameT like '%", toolStripTextBox2.Text, "%'");
            dataGridView1.DataSource = dv;
        }
        private void cellclick_edit()
        {
            int xx = dataGridView1.SelectedCells[0].ColumnIndex;
            object y = dataGridView1.SelectedCells[0].Value; 
            int yy = dataGridView1.SelectedCells[0].RowIndex;
            object o123 = dataGridView1.Rows[yy].Cells[4].Value; 
            object ov = "";
            string codSc = nameCod.readCod( dataGridView1.Rows[yy].Cells[0].Value.ToString());

            string sql = "";

            if (xx == 0) { }  //наименование продукции
            else if (xx == 1)  //кол выход
                 {
                     ov = dataGridView1.Rows[yy].Cells[1].Value;
                     ETALON.FormKolZakaz fk = new ETALON.FormKolZakaz("Введите количество", ov.ToString()); fk.ShowDialog();
                     if(fk.IsSelect)
                     {
                         sql = string.Concat("call `city`.`rz_update_mainlistr_sc` (", fk.valueEnter, ", ", o123, ");"); q.UpdateData(sql);
                         toolStripTextBox1.Text = codSc;
                         //refresh(H.i(codSc));
                         //refreshIf();
                     }
                 }
            else if (xx == 2) //наименование тех сырья
            {
                DataTable dt = q.selectAny("SELECT cod, name FROM list_sc;");
               // ETALON.listAny f = 
                 //   new ETALON.listAny(dt); f.ShowDialog();

                ETALON.listAnyEdit f =
   new ETALON.listAnyEdit("city", "list_sc", "cod<>888"); f.ShowDialog();
                if (f.IsOK)
                {
                    string s1 = string.Concat(
                        "call `city`.`rz_update_mainlistr_tehSir` (", 
                        f.sCod, " , ",
                        o123, ");"); 
                    q.UpdateData(s1);
                    toolStripTextBox1.Text =
                        codSc;
                   // dataGridView1.SelectedCells[0].Value = fk.valueEnter;
                    //refresh(H.i(codSc)); 
                }
            }
            else if (xx == 3)  //кол сырья
                 {
                     ov = dataGridView1.Rows[yy].Cells[3].Value;
                     ETALON.FormKolZakaz fk = 
                     new ETALON.FormKolZakaz("Количество сырья", ov.ToString());
                     fk.ShowDialog();
                     if (fk.IsSelect)
                     {
                         sql = string.Concat("update mainlistr set kol = ", fk.valueEnter ," where cod123 = ",o123,";");
                         q.UpdateData(sql);
                         //refresh(H.i(codSc));
                         dataGridView1.SelectedCells[0].Value = fk.valueEnter;
                     }
            }
            else if (xx == 4)   //код123 
                 { }
        }

        private void editValue()
        {
        }

        private void cellclick_del()
        {
            vv.rz.currentCod123 = 0;
            vv.rz.currentCod123 = dataGridView1.SelectedRows[0].Cells[4].Value; //h.m(o123);
            vv.rz.currentNameCodSc = q.selectScalar(
               string.Concat("select codSc from mainlistr where cod123 = ", vv.rz.currentCod123, ";"));

            q.UpdateData(rz.sql.del(Convert.ToInt32(vv.rz.currentCod123))); refresh(H.i(vv.rz.currentNameCodSc));
        }
        private void cellclick_work()
        {
            vv.rz.currentCod123 = dataGridView1.SelectedRows[0].Cells[4].Value;
            vv.rz.currentNameCodSc =  dataGridView1.SelectedRows[0].Cells[0].Value;
            
            vv.rz.kolV = dataGridView1.SelectedRows[0].Cells[1].Value;

            // H.m(vv.rz.currentCodSc, " ", vv.rz.currentCod123 );
           
            vv.rz.dv_Copy = new DataView(dts); 
            vv.rz.dv_Copy.RowFilter = H.a("name = '", vv.rz.currentNameCodSc, "'");
          


        }
        private void insertIntoRZ()
        {
 
        }


        private void cellclick_non()
        {

        }

        private void setCurrent()
        {
            vv.rz.currentColumn = dataGridView1.SelectedColumns[0].Index;
            vv.rz.currentRow = dataGridView1.SelectedRows[0].Index;
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.add);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.delete );
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.edit);
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cellclick();
        }

        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "- (888)"; 
            vv.rz.add_prod_default = "- (888)";

            setMode(city7.rzpt.modeWAED.work);
        }

        

        private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            refreshIf();
        }

        void refreshIf()
        {
            if (toolStripTextBox1.Text.Length > 0)
            { 
                //refresh(toolStripTextBox1.Text);
                vv.rz.currentNameCodSc = toolStripTextBox1.Text;
                refreshCopy();
            }
         
        }
        void refreshIf2()
        {
            if (toolStripTextBox2.Text.Length > 0)
            { refresh2(toolStripTextBox2.Text); }

        }
 

        private void toolStripStatusLabel1_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel1.DisplayStyle = ToolStripItemDisplayStyle.Text;
        }

        private void toolStripStatusLabel1_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel1.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            toolStripStatusLabel1.Text = "- (888)"; vv.rz.add_prod_default = "- (888)";
        }

        private void FormRzpt_FormClosed(object sender, FormClosedEventArgs e)
        {
            q.con.cc();
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
        }
        private int cf(int nc, int w)
        {
             return nc * w; 
        }
        private void RZPT_print(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            Rectangle r1, r2, r3, r4, r5;
            Rectangle rg;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int wg = 330, ww = 30;
            int w1 = 30, w2 = 0, w3 = 0, w4 = 250, w5 = 50;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Рецептуры цеха для кафэ СИТИ ");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0; 
            //okosh = dts.Compute("SUM(kolv)", "kolv>=0");
            //okol = dts.Compute("SUM(kol)", "kol>=0");
            //---------------------- Итоги
            #region r1---r21

            r1 = new Rectangle(x+vv.RPT.col*330, y, w1, h);
            //r2 = new Rectangle(x + w1, y, w2, h);
            //r3 = new Rectangle(x + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + vv.RPT.col * (wg+ww) + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

            #endregion

            #region drawStr
            //g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r2);
            //g.DrawString(okosh.ToString(), new Font("Arial", f), Brushes.Black, r3, sfc);
            //g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r1 = new Rectangle(x + vv.RPT.col * (wg + ww), y, w1, h);
            //r2 = new Rectangle(x + w1, y, w2, h);
            //r3 = new Rectangle(x + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

            g.DrawRectangle(Pens.LightSteelBlue, r1);
            //g.DrawRectangle(Pens.LightSteelBlue, r2);
            //g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            g.DrawRectangle(Pens.LightSteelBlue, r5);


            #endregion

            #region drawStr
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("Наименование сырья", new Font("Arial", f), Brushes.Black, r4, sfc);
            g.DrawString("кол", new Font("Arial", f), Brushes.Black, r5, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {
                
                if (vv.RPT.lp == 0)
                {
                    rg = new Rectangle(x + vv.RPT.col * (wg + ww), y, wg, hg);
                    vv.RPT.stringControlGroup = n(dts.Rows[linesPrint][0]).ToString();
                    g.DrawString(vv.RPT.stringControlGroup, new Font("Arial", f), Brushes.Black, rg);
                    y = y + h; vv.RPT.lp++;
                }
                else
                {
                    if (vv.RPT.stringControlGroup == n(dts.Rows[linesPrint][0]).ToString())
                            {
                        #region r1---r21

                                r1 = new Rectangle(x + vv.RPT.col * (wg + ww), y, w1, h);
                                r4 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3, y, w4, h);
                                r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

                        g.DrawRectangle(Pens.LightSteelBlue, r1);
                        g.DrawRectangle(Pens.LightSteelBlue, r4);
                        g.DrawRectangle(Pens.LightSteelBlue, r5);

                        #endregion

                        #region drawStr
                        g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r1);
                        g.DrawString(n(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r4);
                        g.DrawString(n(dts.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r5);
                        #endregion

                        y += h; linesPrint++; vv.RPT.lp++;
                        if ((y >= e.PageBounds.Height - 50) && vv.RPT.col > 0)
                        { e.HasMorePages = true; vv.RPT.col = 0; return; }
                        else if (y >= e.PageBounds.Height - 50)
                        { y = vv.RPT.yb; vv.RPT.col++; }
                    }
                    else
                    {

                        rg = new Rectangle(x + vv.RPT.col * (wg + ww), y, wg, hg);
                        vv.RPT.stringControlGroup = n(dts.Rows[linesPrint][0]).ToString();
                        g.DrawString(vv.RPT.stringControlGroup, new Font("Arial", f), Brushes.Black, rg);
                        y = y + h; vv.RPT.lp++;
                        if (y >= e.PageBounds.Height - 50 && vv.RPT.col > 0)
                        { e.HasMorePages = true; vv.RPT.col = 0; return; }
                        else if (y >= e.PageBounds.Height - 50)
                        { y = vv.RPT.yb; vv.RPT.col++; }
                    }
                }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }

        private void RZPT_reestr_print(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            Rectangle r1, r2;
            Rectangle rg;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int wg = 330, ww = 30;
            int w1 = 30, w2 = 300;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Реестр продукции с рецептурами по кафэ СИТИ ");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;
            //okosh = dts.Compute("SUM(kolv)", "kolv>=0");
            //okol = dts.Compute("SUM(kol)", "kol>=0");
            //---------------------- Итоги
            #region r1---r21

            r1 = new Rectangle(x + vv.RPT.col * 330, y, w1, h);
            r2 = new Rectangle(x + w1, y, w2, h);
            
            #endregion

            #region drawStr
            //g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r2);
            //g.DrawString(okosh.ToString(), new Font("Arial", f), Brushes.Black, r3, sfc);
            //g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r1 = new Rectangle(x + vv.RPT.col * (wg + ww), y, w1, h);
            r2 = new Rectangle(x + w1, y, w2, h);
            
            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
           

            #endregion

            #region drawStr
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("Наименование ", new Font("Arial", f), Brushes.Black, r2, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {
                 
                #region r1---r21

                r1 = new Rectangle(x + vv.RPT.col * (wg + ww), y, w1, h);
                r2 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1, y, w2, h);

                g.DrawRectangle(Pens.LightSteelBlue, r1);
                g.DrawRectangle(Pens.LightSteelBlue, r2);


                #endregion

                #region drawStr
                g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r1);
                g.DrawString(n(dts.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r2);
                #endregion

                y += h; linesPrint++; vv.RPT.lp++;
                if ((y >= e.PageBounds.Height - 50) && vv.RPT.col > 0)
                { e.HasMorePages = true; vv.RPT.col = 0; return; }
                else if (y >= e.PageBounds.Height - 50)
                { y = vv.RPT.yb; vv.RPT.col++; }
                    
                
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }
        private void RZPT_print0(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
            Rectangle r1, r2, r3, r4, r5;

            Rectangle rs;
            Rectangle rHeadline, rPageNumber;
            int w1 = 30, w2 = 300, w3 = 40, w4 = 300, w5=50;
            int h = 15, hh = 30, f = 8, ff = 10;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Рецептуры цеха для кафэ СИТИ ");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0; okosh = dts.Compute("SUM(kolv)", "kolv>=0"); okol = dts.Compute("SUM(kol)", "kol>=0");
            //---------------------- Итоги
            #region r1---r21

            r1 = new Rectangle(x, y, w1, h);
            r2 = new Rectangle(x + w1, y, w2, h);
            r3 = new Rectangle(x + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + w1 + w2 + w3 + w4, y, w5, h);

            #endregion

            #region drawStr
            g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, r2);
            g.DrawString(okosh.ToString(), new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r1 = new Rectangle(x, y, w1, h);
            r2 = new Rectangle(x + w1, y, w2, h);
            r3 = new Rectangle(x + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + w1 + w2 + w3, y, w4, h);
            r5 = new Rectangle(x + w1 + w2 + w3 + w4, y, w5, h);

            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
            g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            g.DrawRectangle(Pens.LightSteelBlue, r5);


            #endregion

            #region drawStr
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("Наименование продукции", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("кол вых", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("Наименование сырья", new Font("Arial", f), Brushes.Black, r4, sfc);
            g.DrawString("кол", new Font("Arial", f), Brushes.Black, r5, sfc);
            #endregion
            //----------------------

            y = y + h;
            while (linesPrint < ic)
            {

                #region r1---r21

                r1 = new Rectangle(x, y, w1, h);
                r2 = new Rectangle(x + w1, y, w2, h);
                r3 = new Rectangle(x + w1 + w2, y, w3, h);
                r4 = new Rectangle(x + w1 + w2 + w3, y, w4, h);
                r5 = new Rectangle(x + w1 + w2 + w3 + w4, y, w5, h);

                g.DrawRectangle(Pens.LightSteelBlue, r1);
                g.DrawRectangle(Pens.LightSteelBlue, r2);
                g.DrawRectangle(Pens.LightSteelBlue, r3);
                g.DrawRectangle(Pens.LightSteelBlue, r4);
                g.DrawRectangle(Pens.LightSteelBlue, r5);

                #endregion

                #region drawStr
                g.DrawString(n(linesPrint + 1), new Font("Arial", f), Brushes.Black, r1);
                g.DrawString(n(dts.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r2);
                g.DrawString(n(dts.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r3);
                g.DrawString(n(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r4);
                g.DrawString(n(dts.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r5);
                #endregion

                y += h; linesPrint++;
                if (y >= e.PageBounds.Height - 50) { e.HasMorePages = true; return; }

            }
            linesPrint = 0; e.HasMorePages = false;
        }
        private string nn(object o)
        {
            string s = "0";
            if (Convert.ToInt32(o) < 10) { s = string.Concat("0", o); } else { s = o.ToString(); }
            return s;
        }
        private string n(object o)
        {
            string s = "";
            if (o.ToString() != "0" && o.ToString() != "-") { s = o.ToString(); }
            return s;
        }
        private void RZPT()
        {
            currentPage = 0;
            dts = q.selectAny("call `city`.`select_rez`();");
            vv.RPT.currentReport = vv.RPT.report_RezPodr;
            printPreviewDialog1.ShowDialog();
        }
        private void RZPTr()
        {
            currentPage = 0;
            dts = q.selectAny("CALL select_rez_ree();");
            vv.RPT.currentReport = vv.RPT.report_RezReestr;
            printPreviewDialog1.ShowDialog();
        }
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (vv.RPT.currentReport == vv.RPT.report_RezPodr)
            { RZPT_print(e); }
            else if (vv.RPT.currentReport == vv.RPT.report_RezReestr)
            { RZPT_reestr_print(e); }

            
        }

        private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            currentPage = 0; vv.RPT.col = 0;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            vv.RPT.stringControlGroup = ""; vv.RPT.lp = 0; vv.RPT.yb = 0; vv.RPT.col = 0;
            RZPT();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            vv.RPT.stringControlGroup = "";  vv.RPT.yb = 0; vv.RPT.col = 0;
            
            RZPTr();
        }

        private void toolStripTextBox2_KeyUp(object sender, KeyEventArgs e)
        {
            refreshIf2();
        }

        private void toolStripDropDownButton4_Click(object sender, EventArgs e)
        {
            //if (vv.rz.pasteTrue == false)
            //{

            //}
            vv.rz.pasteTrue = true; toolStripDropDownButton2.Enabled = vv.rz.pasteTrue;
            refreshCopy();

        }

        private void toolStripDropDownButton2_Click(object sender, EventArgs e)
        {
            ETALON.listAnyEdit l = new ETALON.listAnyEdit("city", ETALON.city.list_sc, true);
            l.ShowDialog();

           // H.m(vv.rz.pasteTrue, "=pasteTrue      l.IsOK=", l.IsOK);
            if (l.IsOK)
            {
                vv.rz.currentNameCodSc = l.sText;
                //vv.rz.currentCodSc = l.sCod;
               
                if (vv.rz.pasteTrue == true)
                {
                   // q.UpdateData(H.a("DELETE FROM city.mainlistr_sc where codsc =", vv.rz.currentCodSc));
                    q.UpdateData(H.a("DELETE FROM city.mainlistr where codsc =", vv.rz.currentCodSc));

                    for (int i = 0; i < vv.rz.dv_Copy.Count; i++)
                    {
                        // name,kolV, nameT, kol, cod123
                        q.UpdateData
                            (
                            vv.rz.sql.pasteCopyRZ
                               (
                                 vv.rz.currentCodSc,
                                 nameCod.readCod(vv.rz.dv_Copy[i][2].ToString()),
                                 vv.rz.dv_Copy[i][3]
                                )
                             );
                        //H.m(vv.rz.sql.pasteCopyRZ
                        //       (
                        //         vv.rz.currentCodSc,
                        //         nameCod.readCod(vv.rz.dv_Copy[i][2].ToString()),
                        //         vv.rz.dv_Copy[i][3]
                        //        ));
                    }
                    q.UpdateData(vv.rz.sql.pasteCopyRZvihod(vv.rz.currentCodSc, vv.rz.kolV));
                }
                refresh();
            }
        }

        private void toolStripDropDownButton5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(H.a("Будет удалена рецептура на продукцию ", vv.rz.currentNameCodSc ),"Подтверждение",  MessageBoxButtons.OKCancel ) == DialogResult.OK)
            {
                q.UpdateData(H.a("DELETE FROM mainlistr_sc WHERE CODSC =", vv.rz.currentCodSc));
                q.UpdateData(H.a("DELETE FROM mainlistr WHERE CODSC =", vv.rz.currentCodSc));
                refresh();
            }

        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            city7.table.FormTable f = new city7.table.FormTable((int)city7.table.modeLoad.mainListSC);
            f.ShowDialog(); //H.m(12);
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

          //  MessageBox.Show("Error happened " + e.Context.ToString());
            if (e.Context == DataGridViewDataErrorContexts.Commit)
            {
                
            }


        }

       

      
        

        
    }
}
